package cn.maxwell.org.controller;

import cn.maxwell.basic.annotation.PreAuthorize;
import cn.maxwell.basic.util.JsonResult;
import cn.maxwell.basic.util.PageList;
import cn.maxwell.org.domain.Department;
import cn.maxwell.org.query.DepartmentQuery;
import cn.maxwell.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping //get请求
    @ApiOperation(value = "查询所有" )
    @PreAuthorize(sn="department:getAll",name="查询所有部门")
    public List<Department> loadAll(){
        return departmentService.loadAll();
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}") //get请求，携带id参数
    @ApiOperation(value = "通过ID查询" )
    //swagger注解：paramType="path"：解析path路径携带的参数；dataType="Long"：指定参数类型
    @ApiImplicitParam(name="id",value="编号",required = true,paramType="path",dataType="Long")
    @PreAuthorize(sn="department:getOne",name="查询部门")
    public Department loadById(@PathVariable("id") Long id){//@PathVariable：获取path路径携带的参数
        return departmentService.loadById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除操作" )
    @PreAuthorize(sn="department:delete",name="删除部门")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * 添加或修改
     * @param department
     * @return
     */
    @PutMapping()
    @ApiOperation(value = "添加或修改操作" )
    @PreAuthorize(sn="department:save",name="保存部门")
    public JsonResult save(@RequestBody Department department){//@RequestBody:在请求体中获取参数
        try {
            if(department.getId()==null){
                departmentService.add(department);
            }else{
                departmentService.update(department);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * @param departmentQuery
     * @return
     */
    @PostMapping()
    @ApiOperation(value = "分页查询" )
    @PreAuthorize(sn="department:list",name="部门列表")
    public PageList<Department> query(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public JsonResult patchDelete(@RequestBody List<Long> ids){ //List<Long>:要删除的id的集合
        try {
            departmentService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 部门树
     * @return
     */
    @GetMapping("/deptTree")
    @PreAuthorize(sn="department:deptTree",name="部门树")
    public List<Department> deptTree(){

        return departmentService.deptTree();
    }
}
